package cultu.core.dao.jpa;

import javax.persistence.NoResultException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cultu.common.dao.jpa.UserDAOjpa;
import cultu.core.dao.RoleDAO;
import cultu.core.message.Role;
import cultu.core.message.RoleCriteria;
import cultu.core.message.RoleScenario;
import cultu.framework.dao.jpa.GenericDAOjpa;

/**
 * The Class RoleDAOjpa.
 */
public class RoleDAOjpa extends GenericDAOjpa<Role, RoleCriteria, RoleScenario> implements RoleDAO
{

  /** The Constant LOG. */
  private static final Log LOG = LogFactory.getLog(UserDAOjpa.class);
  
  /**
   * @see cultu.framework.dao.BaseDAO#get(java.lang.Object, java.lang.Object)
   */
  public Role get(Role t, RoleScenario s)
  {
    Role result = null;
    
    if(t != null)
    {
      if(t.getHjid() != null)
      {
         result = get(t.getHjid());
      }
      else
      {
        result = getByCustomKey(t.getCode());
      }
    }
    
    return result;
  }

  /**
   * Gets the by custom key.
   *
   * @param code the code
   * @return the by custom key
   */
  private Role getByCustomKey(String code)
  {
    Role result = null;
    
    if(code != null && !"".equals(code))
    {
      try
      {
        result = (Role) entityManager.createQuery("SELECT r FROM Role r WHERE r.code = : code")
                              .setParameter("code", code)
                              .getSingleResult();
      }
      catch(NoResultException nre)
      {
        LOG.info(nre.getMessage(), nre);
      }
    }
    
    return result;
  }

  /**
   * @see cultu.framework.dao.jpa.GenericDAOjpa#update(cultu.core.message.BaseEntity)
   */
  @Override
  public Role update(Role t)
  {
    // TODO Auto-generated method stub
    return null;
  }

  /**
   * @see cultu.framework.dao.jpa.GenericDAOjpa#validateEntity(cultu.core.message.BaseEntity)
   */
  @Override
  protected boolean validateEntity(Role pEntity)
  {
    // TODO Auto-generated method stub
    return false;
  }
  
}
